package lab;

import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WindowsProgramManager extends ProgramManager{

	@Override
	public String getExecJava() {
		return "java.exe";
	}

	@Override
	public void kill(Program p) {
		try {
			List<String> msgs = CmdUtil.execute("cmd /c tasklist", p.getExecJava()+"[\\w\\W]*");
			if(msgs!=null && msgs.size()==1){
				String line = msgs.get(0);
				Pattern ptn = Pattern.compile("("+p.getExecJava().replaceAll("\\.", "\\\\.")+"\\s+(\\d+))");
				Matcher m = ptn.matcher(line);
				if(m.find()){
					String pid = m.group(2);
					System.out.println("kill process "+p.getExecJava()+",process id="+pid);
					List<String> result = CmdUtil.execute("tskill "+pid);
					if(result!=null){
						for(String l : result){
							System.out.println(l);
						}
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void status(Program p) {
	}

}
